<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<html>
<head>
	<title>客户档案管理</title>
	<script>
		$(document).ready(function() {
			//聚焦第一个输入框
			$("#company_number").focus();
			//为inputCompanyForm注册validate函数
			$("#inputCompanyForm").validate({
				rules: {
					loginName: {
						remote: "${ctx}/register/checkLoginName"
					}
				},
				messages: {
					loginName: {
						remote: "用户登录名已存在"
					}
				}
			});
			$("#addcs_btn").click(addcs_btn_click);
			$("#addcs_submit_btn").click(addcs_submit_btn_click);
			$("#company_standard").change(company_standard_change); 
		});
		function addcs_btn_click(){
			$.post(
				'${ctx}/company/getEnabledCompanyCategoryStandard',
				{exludeIdList:filter_standards()},
				function(data) {
					$("#company_standard").empty();
					$("#company_category").empty();
					$.each(data, function(key, value){      
						$("#company_standard").append("<option value='" + key + "'>" + value +"</option>");
					});
					$("#company_standard").get(0).selectedIndex=0;
					company_standard_change();
			});
			$("#companyCategorys_alert").show();
		}
		function company_standard_change(){
			var checkValue=$("#company_standard").val();
			$.ajax({
				url:'${ctx}/company/getEnabledCompanyCategoryByStd/'+checkValue, 
				success:function(data) {
					$("#company_category").empty();
					$.each(data, function(key, value){
						$("#company_category").append("<option value='" + key + "'>" + value +"</option>");
					});  
					$("#company_category").get(0).selectedIndex=0;
			}});
		}
		function addcs_submit_btn_click(){
			var checkStandardValue=$("#company_standard").val(),
				checkStandardText=$("#company_standard").find("option:selected").text(),
			 	checkCategoryValue=$("#company_category").val(),
			 	checkCategoryText=$("#company_category").find("option:selected").text(),
			 	len = $("#companyCategorys tr").length,
			 	index = len - 1,
			 	hiddenInput = '<td style="display:none"><input type="text" name="css" value="'+checkStandardValue+'"/><input type="text" name="companyCategorys['+ index + '].id" value="'+checkCategoryValue+'"/></td>';
			 	
			if(!checkCategoryValue || checkCategoryValue == ''){
				alert('选择的客户分类不能为空。');
				return;
			} 	
			
	        var rowTemplate = '<tr id="cc_'+ index +'"><td>'+checkStandardText+'</td><td>'+checkCategoryText+'</td><td><a href="#" onclick="delRow('+index+')">删除</a></td>'+hiddenInput+'</tr>';
	        var tableHtml = $("#companyCategorys tbody").html();
	        tableHtml += rowTemplate;
	        $("#companyCategorys tbody").html(tableHtml);
	        $('#companyCategorys_alert').hide();
		}
	   	function delRow(row_id){
	   		//删除当前行
			$("tr[id='cc_"+row_id+"']").remove();
		}
	   	function filter_standards(){
	   		var csArray = $('input[name="css"]'),
	   			ids = new Array();
	   		if(csArray){
		   		$.each(csArray, function(i, cs){
					ids.push(cs.value);
				});
	   		}
			return ids;
	   	}
	</script>
</head>

<body>
	<form id="inputCompanyForm" enctype="multipart/form-data" action="${ctx}/company/${action}" method="post" class="form-horizontal">
		<input type="hidden" name="id" value="${company.id}"/>
		<fieldset>
			<legend><small>管理客户档案</small></legend>
			<div class="control-group">
				<label for="company_number" class="control-label">客户编码:</label>
				<div class="controls">
					<input type="text" id="company_number" name="number" value="${company.number}" class="input-large required" minlength="3"/>
				</div>
			</div>	
			<div class="control-group">
				<label for="company_name" class="control-label">客户名称:</label>
				<div class="controls">
					<input type="text" id="company_name" name="name" value="${company.name}" class="input-large required" minlength="3"/>
				</div>
			</div>
			<div class="control-group">
				<table id="companyCategorys" class="table table-striped table-bordered table-condensed">
					<thead><tr><th>客户分类标准</th><th>客户分类</th><th>操作</th></tr></thead>
					<tbody>
						<c:forEach var="companyCategory" items="${company.companyCategorys}" varStatus="loopStatus">
								<tr id="cc_${loopStatus.index}">
									<td>${companyCategory.std.name}</td>
									<td>${companyCategory.name}</td>
									<td><a href="#" onclick="delRow(${loopStatus.index})">删除</a></td>
									<td style="display:none">
										<input type="text" name="css" value="${companyCategory.std.id}" />
										<spring:bind path="company.companyCategorys[${loopStatus.index}]">
											<input type="text" name="${status.expression}.id" value="${status.value.id}" />
										</spring:bind>
									</td>
								</tr>
							
						</c:forEach>
					</tbody>
				</table>
			</div>
			<div id="companyCategorys_alert" class="alert alert-info" style="display:none">
				<div class="control-group">
					<label for="company_standard" class="control-label">客户分类标准:</label>
					<div class="controls">
					<select id="company_standard"></select>
					</div>
				</div>
				<div class="control-group">
					<label for="company_standard" class="control-label">客户分类:</label>
					<div class="controls">
						<select id="company_category"></select>
					</div>
				</div>
				<div class="form-actions">
					<input id="addcs_submit_btn" class="btn btn-primary" type="button" value="确定"/>&nbsp;
					<input id="addcs_cancel_btn" class="btn" type="button" value="取消" onclick="$('#companyCategorys_alert').hide()"/>
				</div>
			</div>
			
			<c:forEach var="companyLinkman" items="${company.companyLinkmans}" varStatus="loopStatus">
				<spring:bind path="company.companyLinkmans[${loopStatus.index}]">
					<input type="hidden" id="companyLinkmans_id" name="${status.expression}.id" value="${status.value.id}"/>
					<input type="hidden" id="companyLinkmans_company_id" name="${status.expression}.company.id" value="${company.id}"/>
					<div class="control-group">
						<label for="company_Linkman_name" class="control-label">联系人:</label>
						<div class="controls">
							<input type="text" name="${status.expression}.name" value="${status.value.name}" class="input-large required" minlength="2"/>
						</div>
					</div>
					<div class="control-group">
						<label for="company_Linkman_phone" class="control-label">联系电话:</label>
						<div class="controls">
							<input type="text" name="${status.expression}.phone" value="${status.value.phone}" class="input-large required" minlength="3"/>
						</div>
					</div>
					<div class="control-group">
						<label for="company_Linkman_email" class="control-label">电子邮件:</label>
						<div class="controls">
							<input type="text" name="${status.expression}.email" value="${status.value.email}" class="input-large required" minlength="3"/>
						</div>
					</div>
				</spring:bind>
			</c:forEach>
			
			<div class="control-group">
				<label for="company_remark" class="control-label">备注:</label>
				<div class="controls">
					<textarea id="company_remark" name="remark" class="input-large">${company.remark}</textarea>
				</div>
			</div>
			<div class="form-actions">
				<input id="submit_btn" class="btn btn-primary" type="submit" value="提交"/>&nbsp;	
				<input id="cancel_btn" class="btn" type="button" value="返回" onclick="history.back()"/>&nbsp;
				<input id="addcs_btn" class="btn" type="button" value="新增分类关系"/>
			</div>
		</fieldset>
	</form>
</body>
</html>